package com.bzy.blade_auth.config.util;

import com.alibaba.fastjson.JSON;
import org.checkerframework.checker.units.qual.s;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class ParmUtil {


    private final static String WRONG_PARM = "wrong_parm";
    private final static String ERROR_MSG = "缺少参数";


    /**
     * 参数教验;
     * 返回null为不缺少参数、返回的String(不为null)为缺少的参数的信息数据;
     *
     * @return
     */
    public static String parameterIntended(Object obj, String... parm) {
        //将需要教验的参数信息转为list;
        List<String> parmList = Arrays.asList(parm);
        //将参数对象转化为map;
        Map<String, Object> mapObj = JSON.parseObject(JSON.toJSONString(obj), Map.class);
        //获取所有的key值;
        Set<String> keyList = mapObj.keySet();
        if (keyList.size() == 0 && parmList.size() != 0) {
            return ERROR_MSG + parmList;
        }
        //判断当前的对象当中是否有需要的参数信息;
        Set<String> collectSetList = parmList.stream().map(s -> {
            if (keyList.contains(s)) {
                return WRONG_PARM;
            } else {
                return s;
            }
        }).filter(m -> !m.equals(WRONG_PARM)).collect(Collectors.toSet());
        if (collectSetList.size() > 0) {
            return ERROR_MSG + collectSetList;
        }
        return null;
    }


    public static Boolean isNull(Object o){
        return o==null;
    }
}













